﻿Imports log4net
Imports NHibernate.Tool.hbm2ddl

Public Class SessionFactory

    Private Shared _SessionFactory As NHibernate.ISessionFactory

    Private Shared Sub Init()

        Dim config As NHibernate.Cfg.Configuration

        config = New NHibernate.Cfg.Configuration
        config.AddAssembly("ACompany.OrderProcessing.NhibernateRepository")

        log4net.Config.XmlConfigurator.Configure()

        ' As long as we have an empty database with the same name as the one in our 
        ' connection string Nhibernate will generate the schema for us, we only want
        ' to run this once.
        'Dim se As New SchemaExport(config)
        'se.Execute(False, True, False, False)

        config.Configure()

        _SessionFactory = config.BuildSessionFactory

    End Sub

    Public Shared Function GetSessionFactory() As NHibernate.ISessionFactory
        If _SessionFactory Is Nothing Then
            Init()
        End If

        Return _SessionFactory
    End Function

    Public Shared Function GetNewSession() As NHibernate.ISession

        Return GetSessionFactory.OpenSession

    End Function

    Public Shared Function GetCurrentSession() As NHibernate.ISession

        Return GetSessionFactory.GetCurrentSession

    End Function

End Class